package controllers;

import play.mvc.*;
import com.mysql.jdbc.Connection;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import models.Exercise;
import models.Notification;
import models.RegisteredUser;
import models.Score;
import models.Topic;
import models.Tutorial;

public class RegisteredUsers extends Controller {

	public static void index() {
		render();
	}

	/**
	 * This method calls the method followUser in Model RegisteredUser and
	 * checks if the followed User is in the followedUser list, it renders text
	 * that the user is already following this user, if not it renders text that
	 * the user has successfully followed this user
	 * 
	 * @param fId
	 *            which is the id of the followed user.
	 * 
	 * @return rendered Text if the user is not logged in, if he's already
	 *         following this user and if he successfully followed the user
	 * 
	 * @author Marwa
	 * @author Omnia - Notification
	 */
	public static void followUsr(long fId) {
		if (session.get("RegisteredUserId") != null) {
			long userId = Long.parseLong(session.get("RegisteredUserId"));
			if (fId != userId) {
				RegisteredUser followedUser = RegisteredUser.findById(fId);
				boolean result = followedUser.followUser(userId, fId);
				if (result == true) {
					new Notification(userId, fId, 0, 30).save();
					renderText("You have successfully followed this User");
				} else {
					renderText("You Are already following this User");
				}
			} else {
				renderText("You cannot follow Yourself :)");
			}
		} else {
			renderText("You must Log in first to follow a user!!");
		}
	}

	/**
	 * this method views a list of followed users it contains a condition were
	 * it checks first if it is the registered user who is viewing or others.
	 * 
	 * @author Nada
	 * @param viewOthers
	 */

	public static void followedUserPage(long viewOthers) {
		long registeredUserId = Long.parseLong(session.get("RegisteredUserId"));
		RegisteredUser user = RegisteredUser.findById(registeredUserId);
		RegisteredUser otherUser = RegisteredUser.findById(viewOthers);
		if (user != otherUser) {
			List<RegisteredUser> followedUsers = otherUser
					.viewFollowedUsers(otherUser.id);
			render(followedUsers);
		} else {
			List<RegisteredUser> followedUsers = user
					.viewFollowedUsers(registeredUserId);
			render(followedUsers);
		}
	}

	// Start @ author: Nada
	// this method returns the number of points the user gained through
	// creating tutorials, or reviewing a tutorial .
	// It first checks the session id (user's id) and then gets his calculated
	// points.
	// u.isReviewer () this method enables a user to be a reviewer after having
	// equal to or more than 100 points
	// it gets the user's id and checks his points
	// param x, x is the number of points
	// @ end author: Nada

	public static void checkMyPoints(long viewOthers) {

		long uid = Long.parseLong(session.get("RegisteredUserId"));
		RegisteredUser u = RegisteredUser.findById(uid);
		RegisteredUser other = RegisteredUser.findById(viewOthers);

		if (u != other) {
			int otherScore = other.getPoints();
			renderText(otherScore);

		} else {

			int x = u.getPoints();
			if (x >= 100) {
				u.isReviewer();
			}
			renderText(x);
		}
		// else{
		// int Score = u.getPoints();
		// renderText(Score);
		// }
	}

	/**
	 * This method allows the user to set his profile to public and checks if it
	 * was already set to private then it changes it to false
	 * 
	 * @author Nada
	 * @param selection
	 */
	public static void setPublic(long selection) {
		long userId = Long.parseLong(session.get("RegisteredUserId"));
		RegisteredUser user = RegisteredUser.findById(userId);

		user.setPublic(true);
		user.setPrivate(false);

		setPrivacy();
	}

	/**
	 * This method allows the user to set his profile to private and checks if
	 * it was already set to public then it changes it to false
	 * 
	 * @author Nada
	 * @param selection
	 */
	public static void setPrivate(long selection) {
		long userId = Long.parseLong(session.get("RegisteredUserId"));
		RegisteredUser user = RegisteredUser.findById(userId);

		user.setPrivate(true);
		user.setPublic(false);

		setPrivacy();
	}

	/**
	 * this method allows the user to check his exercise scores by getting the
	 * users exercises taken and the user's list of scores
	 * 
	 * @author Nada
	 */

	public static void exerciseScores() {

		long userId = Long.parseLong(session.get("RegisteredUserId"));

		List<Score> exercisesList = Score.find("userId=?", userId).fetch();

		render(exercisesList);

	}

	/**
	 * this method returns setPrivacy.html
	 * 
	 * @author Nada
	 */
	public static void setPrivacy() {
		render();
	}
}
